var that;
class Tab {
    constructor(id) {
        //获取元素
        that = this;
        this.main = document.querySelector(id);
        this.add = this.main.querySelector('.tabadd');
        this.ul = this.main.querySelector('.firstnav ul:first-child');
        this.fasection = this.main.querySelector('.tabscon');

        this.init();
    }
    init() {
        this.updateNode();
        //init 初始化操作，让相关的元素绑定事件
        this.add.onclick = this.addTab;
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].index = i;
            this.lis[i].onclick = this.toggleTab;//不要跟小括号，不然页面一加载就直接调用了
            this.remove[i].onclick = this.removeTab;
            this.spans[i].ondblclick = this.editTab;
            this.sections[i].ondblclick = this.editTab;
        }
    }
    //获取所以li和section
    updateNode() {
        this.lis = this.main.querySelectorAll('li');
        this.sections = this.main.querySelectorAll('section');
        this.remove = this.main.querySelectorAll('.icon-guanbi');
        this.spans = this.main.querySelectorAll('.firstnav li span:first-child')
    }
    //1.切换功能
    toggleTab() {
        that.clearClass();
        this.className = 'liactive';
        that.sections[this.index].className = 'conactive';
    }
    clearClass() {
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].className = '';
            this.sections[i].className = '';
        }
    }
    //2.添加功能
    addTab() {
        that.clearClass();
        var random = Math.random();
        //创建li元素和section元素
        var li = '<li class="liactive"><span>新选项</span><span class="iconfont icon-guanbi"></span></li>';
        var section = '<section class="conactive">测试' + random + '</section>';
        //把这两个元素追加到对应的父元素中
        that.ul.insertAdjacentHTML('beforeend', li);
        that.fasection.insertAdjacentHTML('beforeend', section);
        that.updateNode();
        that.init();
    }
    //3.删除功能
    removeTab(e) {
        e.stopPropagation();//阻止冒泡，防止触发父亲
        var index = this.parentNode.index;
        console.log(index);
        that.lis[index].remove();
        that.sections[index].remove();
        that.init();
        //当我们删除的不是选定状态的li时，原来的选定状态的li保持不变
        if (document.querySelector('.liactive')) return;
        //当我们删除了选中状态的这个li，让它的前一个li处于选定状态
        index--;
        //手动点击事件，不用鼠标触发
        that.lis[index] && that.lis[index].click();//前面为真，执行后面的，前面为假，就不执行
    }
    //4.修改功能
    editTab() {
        var str = this.innerHTML;
        //双击禁止选中文字
        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
        this.innerHTML = '<input type="text">';
        var input = this.children[0];
        input.value = str;
        input.select();//文本框处于选定状态
        input.onblur = function () {
            this.parentNode.innerHTML = this.value;
        }
        input.onkeyup = function (e) {
            if (e.keyCode === 13) {
                //自动失去焦点
                this.blur();
            }
        }
    }
}
new Tab('#tab');
